/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.Library;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.LibraryRepository;
import me.zhengjie.service.LibraryService;
import me.zhengjie.service.dto.LibraryDto;
import me.zhengjie.service.dto.LibraryQueryCriteria;
import me.zhengjie.service.mapstruct.LibraryMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author ...
* @date 2020-11-08
**/
@Service
@RequiredArgsConstructor
public class LibraryServiceImpl implements LibraryService {

    private final LibraryRepository libraryRepository;
    private final LibraryMapper libraryMapper;

    @Override
    public Map<String,Object> queryAll(LibraryQueryCriteria criteria, Pageable pageable){
        Page<Library> page = libraryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(libraryMapper::toDto));
    }

    @Override
    public List<LibraryDto> queryAll(LibraryQueryCriteria criteria){
        return libraryMapper.toDto(libraryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LibraryDto findById(Integer libraryId) {
        Library library = libraryRepository.findById(libraryId).orElseGet(Library::new);
        ValidationUtil.isNull(library.getLibraryId(),"Library","libraryId",libraryId);
        return libraryMapper.toDto(library);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LibraryDto create(Library resources) {
        return libraryMapper.toDto(libraryRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Library resources) {
        Library library = libraryRepository.findById(resources.getLibraryId()).orElseGet(Library::new);
        ValidationUtil.isNull( library.getLibraryId(),"Library","id",resources.getLibraryId());
        library.copy(resources);
        libraryRepository.save(library);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer libraryId : ids) {
            libraryRepository.deleteById(libraryId);
        }
    }

    @Override
    public void download(List<LibraryDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (LibraryDto library : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("图书馆名", library.getName());
            map.put("0:官方图书馆  1:个人图书馆", library.getType());
            map.put("纬度", library.getLatitude());
            map.put("经度", library.getLongitude());
            map.put("图书馆所有者的编号 对应reader_id", library.getOwnerId());
            map.put("图书馆后台管理账号的编号对应user_id", library.getUserId());
            map.put("图书馆地址", library.getAddress());
            map.put("是否启用 1启用 0未启用", library.getIsEnable());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}